Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports System.Text
Imports System.String
Imports System.Xml
Imports System.Security.Cryptography
Imports System.Web.HttpResponse
Imports System.Text.RegularExpressions
Imports System.Net.Mail
Imports Microsoft.VisualBasic
Imports System.IO

Public Class CommonUtil
    Public Shared Function getSystemMessage(ByVal page As System.Web.UI.Page) As String
        If page.Session("SYSTEM_MSG") IsNot Nothing Then
            Dim msg As String = CType(page.Session("SYSTEM_MSG"), String)
            page.Session("SYSTEM_MSG") = Nothing
            Return msg
        End If
        Return ""
    End Function
    
    Public Shared Function NunberFormat(ByVal number As Decimal) As String
        Return number.ToString("#,##0.00")
    End Function
    Public Shared Function MoneyFormat(ByVal number As Decimal) As String
        Return number.ToString("#,##0.00")
    End Function
    
    Public Shared Function commonExecuteDataset(ByVal strSql As String) As DataSet
        Dim ds As DataSet
        Dim dbAccess As New DBAccess()
        Dim con As SqlClient.SqlConnection = dbAccess.GetSqlConnection()

        If con Is Nothing Then
            ds = Nothing
        Else
            Try
                ds = SqlHelper.ExecuteDataset(con, CommandType.Text, strSql)
            Finally
                If con IsNot Nothing Then
                    con.Close()
                End If
            End Try
        End If
        Return ds
    End Function
    Public Shared Function commonExecuteNonQuery(ByVal strSql As String) As Boolean
        Dim dbAccess As New DBAccess()
        Dim con As SqlClient.SqlConnection = dbAccess.GetSqlConnection()
        If con Is Nothing Then
            Return False
        Else
            Try
                SqlHelper.ExecuteNonQuery(con, CommandType.Text, strSql)
                If con IsNot Nothing Then
                    con.Close()
                End If
                Return True
            Finally
                If con IsNot Nothing Then
                    con.Close()
                End If
            End Try
        End If
        Return False
    End Function
    Public Shared Function GetConnection(ByVal tstrConnectionString As String) As SqlConnection
        Dim dbconnection As SqlConnection = New SqlConnection(tstrConnectionString)
        dbconnection.Open()
        Return dbconnection
    End Function
    Public Shared Function GetConnection() As SqlConnection
        Dim DBA As New DBAccess()
        Dim dbconnection As SqlConnection = DBA.GetSqlConnection()
        If dbconnection IsNot Nothing Then
            dbconnection.Open()
        End If
        Return dbconnection
    End Function
    Public Shared Function GetConnectionString() As String
        Dim DBA As New DBAccess()
        Return DBA.GetSqlConnection.ConnectionString()
    End Function

    Public Shared Function maxRow(ByVal sql As String, ByVal table As String) As Integer
        Dim nextRow As Integer


        Dim conn As SqlConnection = CommonUtil.GetConnection()
        Dim ds As New DataSet
        Dim Da As New SqlDataAdapter


        Da.SelectCommand = New SqlClient.SqlCommand(Sql, conn)
        Da.Fill(ds, table)

        Dim dts As System.Data.DataTable = ds.Tables(table)

        If dts IsNot Nothing Then
            Dim dr As DataRow
            For Each dr In dts.Rows
                nextRow = CInt(dr("maxRow"))
                nextRow += 1
            Next

        End If

        conn.Close()
        conn.Dispose()
        conn = Nothing

        Return nextRow
    End Function

    Public Shared Function executeSQL(ByVal sql As String, ByVal table As String) As DataSet

        Dim conn As SqlConnection = CommonUtil.GetConnection()
        Dim ds As New DataSet
        Dim Da As New SqlDataAdapter
        Da.SelectCommand = New SqlClient.SqlCommand(sql, conn)
        Da.Fill(ds, table)
        Return ds
        conn.Close()
        conn.Dispose()
        conn = Nothing

    End Function
End Class



